#include <algorithm>
#include <cstdio>

const int N = 20005;
int n, t;
struct Person {
  int a, b;
  inline bool operator<(const Person &rhs) const {
    return ((a >= b) == (rhs.a >= rhs.b)) ? ((a < b) ? a < rhs.a : b > rhs.b)
                                      : ((a >= b) < (rhs.a >= rhs.b));
  }
} p[N];

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("P2123 皇后游戏.in", "r", stdin);
  freopen("P2123 皇后游戏.out", "w", stdout);
#endif
#endif

  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d%d", &p[i].a, &p[i].b);
    std::sort(p + 1, p + n + 1);
    long long c = p[1].a + p[1].b, sum = p[1].a;
    for (int i = 2; i <= n; ++i) {
      sum += p[i].a;
      c = std::max(c, sum) + p[i].b;
    }
    printf("%lld\n", c);
  }
  return 0;
}